package br.com.gsc.vaadin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import br.com.gsc.model.tableMapping.Operator;
import br.com.gsc.model.tableMapping.Person;
import br.com.gsc.model.tableMapping.Post;
import br.com.gsc.repository.objRepos.PersonRepository;
import br.com.gsc.repository.objRepos.ProductRepository;
import br.com.gsc.setup.AuthenticationHolder;

import com.invient.vaadin.charts.InvientCharts;
import com.invient.vaadin.charts.InvientCharts.DecimalPoint;
import com.invient.vaadin.charts.InvientCharts.SeriesType;
import com.invient.vaadin.charts.InvientCharts.XYSeries;
import com.invient.vaadin.charts.InvientChartsConfig;
import com.invient.vaadin.charts.InvientChartsConfig.AxisBase.AxisTitle;
import com.invient.vaadin.charts.InvientChartsConfig.CategoryAxis;
import com.invient.vaadin.charts.InvientChartsConfig.ColumnConfig;
import com.invient.vaadin.charts.InvientChartsConfig.HorzAlign;
import com.invient.vaadin.charts.InvientChartsConfig.NumberYAxis;
import com.invient.vaadin.charts.InvientChartsConfig.XAxis;
import com.invient.vaadin.charts.InvientChartsConfig.XAxisDataLabel;
import com.invient.vaadin.charts.InvientChartsConfig.YAxis;

@Transactional
@Configurable(preConstruction=true, autowire=Autowire.BY_TYPE)
public class OperatorAnswersChart extends GenericGraphWithRightMenu<Operator, List<Post>> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2916447101581847749L;

	@Autowired
	private PersonRepository pRepo;
	@Autowired
	private AuthenticationHolder auth;
	@Autowired
	private ProductRepository proRepo;
	private Map<Operator, List<Post>> mapOfEverything = new HashMap<Operator, List<Post>>();
	private Map<Operator, List<Post>> mapFiltered = new HashMap<Operator, List<Post>>();
	
	public OperatorAnswersChart(){
		
		for(Person person : pRepo.ListAllPeople()){
			if(person instanceof Operator){				
				mapOfEverything.put((Operator) person, ((Operator)person).getPost());
			}
		}
		mapFiltered = mapOfEverything;
		createComponents(mapOfEverything, mapFiltered);
		removeRightMenu();
	}	
	
	@Override
	public InvientCharts setupChart(Map<Operator, List<Post>> map){
		InvientChartsConfig chartConfig = new InvientChartsConfig();
        chartConfig.getGeneralChartConfig().setType(SeriesType.COLUMN);
        chartConfig.getTitle().setText("Topicos respondidos por operador");        
        
        List<String> listOfFullNames = new ArrayList<String>();
        for(Person person : map.keySet()){ 
        	listOfFullNames.add(person.getUsername());
        }
        CategoryAxis xAxis = new CategoryAxis();
        xAxis.setCategories(listOfFullNames);
        xAxis.setLabel(new XAxisDataLabel());
        xAxis.getLabel().setRotation(-45);
        xAxis.getLabel().setAlign(HorzAlign.RIGHT);
        xAxis.getLabel()
                .setStyle("{ font: 'normal 13px Verdana, sans-serif' }");
      	LinkedHashSet<XAxis> xAxesSet = new LinkedHashSet<InvientChartsConfig.XAxis>();
      	xAxesSet.add(xAxis);	      	
      	chartConfig.setXAxes(xAxesSet);
      	
        // Tooltip formatter
//        chartConfig
//                .getTooltip()
//                .setFormatterJsFunc(
//                        "function() { "
////                                + " return '<b>SA   -  Data de Fechamento   - %</b><br/>' +  this.point.name + ' - '+ $wnd.Highcharts.numberFormat(this.y,1 ) +'%'"                                                               ]
//                        		+ " return this.point.name"
//                                + "}");

        NumberYAxis yAxis = new NumberYAxis();
       	yAxis.setTitle(new AxisTitle("Numero de respostas"));
        LinkedHashSet<YAxis> yAxesSet = new LinkedHashSet<InvientChartsConfig.YAxis>();
        yAxesSet.add(yAxis);
        chartConfig.setYAxes(yAxesSet);

        ColumnConfig colCfg = new ColumnConfig();
        colCfg.setPointPadding(0.2);
        colCfg.setPointWidth(20);
        colCfg.setBorderWidth(1);
        chartConfig.addSeriesConfig(colCfg);

        InvientCharts localChart = new InvientCharts(chartConfig);
        
        // Series  Work   
        XYSeries xySeries = new XYSeries("Topicos",SeriesType.COLUMN, colCfg); 
        
        addSeriesToChart(xySeries, localChart, mapOfEverything);
        
        localChart.setSizeFull();

        return localChart;
	}


	private void addSeriesToChart(XYSeries series, InvientCharts chart,Map<Operator, List<Post>> map) {

        for (Operator oper : map.keySet()){
           DecimalPoint point = new DecimalPoint(series,oper.getPost().size());
     	   point.setName("Operador "+oper.getUsername());
     	   series.addPoint(point);
        }
        
        chart.addSeries(series);
	}

}
